﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataSet1TableAdapters;

public partial class int_video : System.Web.UI.Page
{
    RakstiTableAdapter RakstiTa = new RakstiTableAdapter();
    DataSet1.RakstiDataTable RakstiDa = new DataSet1.RakstiDataTable();

    DisplayTableAdapter DisplayTa = new DisplayTableAdapter();
    DataSet1.DisplayDataTable DispayDs = new DataSet1.DisplayDataTable();

    SadalaTableAdapter SadalaTa = new SadalaTableAdapter();
    DataSet1.SadalaDataTable SadalaDs = new DataSet1.SadalaDataTable();

    DataSet1.RakstiDataTable RakstiAll = new DataSet1.RakstiDataTable();

    public string[] FotoUrl = new string[10]; // + ŠITO kodu
    int PageElementNumber = 10;

    protected void Page_Load(object sender, EventArgs e)
    {

        RakstiDa = RakstiTa.GetDataBySortDatums(); // ir jau sasortēti pēc datuma. 
        foreach (DataSet1.RakstiRow Rr in RakstiDa)
        {
            DispayDs = DisplayTa.GetDataByRakstID(Rr.ID_name);
            bool Ir = false;
            if (DispayDs.Rows.Count > 0)
            {
                foreach (DataSet1.DisplayRow Dr in DispayDs)
                {
                    if ((Dr.SadalasID == 17))
                    {
                        Ir = true;
                        break;
                    }
                }
            }
            if (Ir)
            {
                RakstiAll.Rows.Add(Rr.ItemArray);
            }
        }
        ItemsGet();
    }
    private void ItemsGet()
    {
        Array.Resize(ref FotoUrl, 10);
        int ItemMax = 0;
        if (RakstiAll.Count < CurrentPage * PageElementNumber + PageElementNumber)
        {
            ItemMax = RakstiAll.Count - CurrentPage * PageElementNumber;
        }
        else
        {
            ItemMax = 10;
        }

        for (int i = 0; i < ItemMax; i++)
        {
            FotoUrl[i] = RakstiAll[i + CurrentPage * PageElementNumber].bildeTumb;
        }
        Page.DataBind();

        // paging kods ko iekopē
        PagedDataSource objPds = new PagedDataSource();
        objPds.DataSource = RakstiAll.DefaultView;// Tables[0].DefaultView;

        // Indicate that the data should be paged
        objPds.AllowPaging = true;

        // Set the number of items you wish to display per page
        objPds.PageSize = PageElementNumber;

        // Set the PagedDataSource's current page
        objPds.CurrentPageIndex = CurrentPage;// -1;

        lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of "
        + objPds.PageCount.ToString();

        // Disable Prev or Next buttons if necessary
        cmdPrev.Enabled = !objPds.IsFirstPage;
        cmdNext.Enabled = !objPds.IsLastPage;
        Repeater1.DataSource = objPds;

        Repeater1.DataBind();
    }

    public int CurrentPage
    {
        get
        { // look for current page in ViewState
            object o = this.ViewState["_CurrentPage"];
            if (o == null)
                return 0; // default page index of 0
            else
                return (int)o;
        }
        set
        {
            this.ViewState["_CurrentPage"] = value;
        }
    }
    protected void cmdPrev_Click(object sender, EventArgs e)
    { // Set viewstate variable to the previous page
        CurrentPage -= 1;

        // Reload control
        ItemsGet();

    }
    protected void cmdNext_Click1(object sender, EventArgs e)
    {// Set viewstate variable to the next page
        CurrentPage += 1;

        // Reload control
        ItemsGet();
    }

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int rakstanr = e.Item.ItemIndex + CurrentPage * PageElementNumber;
        int rakstaID_nr = RakstiAll[rakstanr].ID_name;
        //     Response.Redirect("DefaultZoom.aspx?value=" + rakstaID_nr);

        string Vr = RakstiAll[rakstanr].Virsraksts;
        char[] SpaceDalitajs = { ' ' };

        string Vr_new = "";
        string Vr_line = "";
        if (Vr.Contains("-") == true)
        {
            string[] Paa = Vr.Split('-');
            Vr_line = Paa[0];
            for (int Pab = 1; Pab < Paa.Length; Pab++)
            {
                Vr_line += "--" + Paa[Pab];
            }
        }
        else { Vr_line = Vr; }
        string[] Vr_sadalits = Vr_line.Split(SpaceDalitajs);
        if (Vr_sadalits.Length != 1)
        {
            Vr_new = Vr_sadalits[0];
            for (int ii = 1; ii < Vr_sadalits.Length; ii++)
            {
                Vr_new += "-" + Vr_sadalits[ii];
            }
        }
        else { Vr_new = Vr_sadalits[0]; }
        Response.Redirect("DefaultZoom.aspx?value=" + Vr_new);
    }
    // ŠOTAS ir JAUNAIS Kods
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            int Nr = e.Item.ItemIndex;
            if (Nr % 2 == 0) //then it's even
            {
                if (FotoUrl[Nr] != "")
                {
                    LinkButton Lnk = ((LinkButton)e.Item.FindControl("LinkButton1"));
                    Lnk.Text = "<img src='" + FotoUrl[Nr] + "' style='float: left' class='bildelink'/> ";
                }
            }
            else
            { //It is odd
                if (FotoUrl[Nr] != "")
                {
                    LinkButton Lnk = ((LinkButton)e.Item.FindControl("LinkButton2"));
                    Lnk.Text = "<img src='" + FotoUrl[Nr] + "' style='float: left' class='bildelink'/> ";
                }
            }
        }
        else if (e.Item.ItemType == ListItemType.AlternatingItem)
        {
            int Nr = e.Item.ItemIndex;
            if (Nr % 2 == 0) //then it's even
            {
                if (FotoUrl[Nr] != "")
                {
                    LinkButton Lnk = ((LinkButton)e.Item.FindControl("LinkButton1"));
                    Lnk.Text = "<img src='" + FotoUrl[Nr] + "' style='float: left' class='bildelink'/> ";
                }
            }
            else
            { //It is odd
                if (FotoUrl[Nr] != "")
                {
                    LinkButton Lnk = ((LinkButton)e.Item.FindControl("LinkButton2"));
                    Lnk.Text = "<img src='" + FotoUrl[Nr] + "' style='float: left' class='bildelink'/> ";
                }
            }
        }
    }
}
